﻿
Imports std

Public Class value_clause
    Inherits sentence

    Public Shadows Const separator As Char = character.equalSign
    Public Const defaultOverwrite As Boolean = True

    Protected Friend Shared Shadows Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return isseparator(c)
    End Function

    Private left As String = Nothing
    Private value As value = Nothing

    Public Function valuename() As String
        Return left
    End Function

    Private Sub New(ByVal ileft As String, ByVal ivalue As value)
        left = ileft
        value = ivalue
    End Sub

    Private Function _invoke(ByVal dest As context, ByVal src As context, _
                             ByVal overwrite As Boolean) As Boolean
        assert(Not dest Is Nothing, "destination context is nothing.")
        assert(Not src Is Nothing, "source context is nothing.")

        Dim v As bare_variable = Nothing
        If value.invoke(src, v) Then
            Dim leftv As bare_variable = Nothing
            If dest.findVariable(left, leftv) Then
                If matchtype(leftv.variable().type(), v.variable().type()) Then
                    If overwrite Then
                        leftv.setvalue(v.value())
                    End If
                    Return True
                Else
                    raiseError("expect type " + typeNaming(v.variable().type()) + " for " + left, _
                               errorHandle.errorType.user)
                    Return False
                End If
            Else
                dest.insertVariable(left, v)
                Return True
            End If
        Else
            Return False
        End If
    End Function

    Protected Overrides Function invokeImpl(Of T)(ByVal dest As context, ByVal src As context, _
                                                  ByRef obj As T) As Boolean
        Return _invoke(dest, src, convertor.cast(Of Boolean)(obj))
    End Function

    Protected Overrides Function invokeImpl(ByVal ctx As context) As Boolean
        Return _invoke(ctx, ctx, defaultOverwrite)
    End Function

    Public Overloads Function invoke(ByVal dest As context, ByVal src As context, _
                                     Optional ByVal overwrite As Boolean = defaultOverwrite) As Boolean
        Return invokebase(dest, src, overwrite)
    End Function

    Private Shared Function isseparator(ByVal c As Char) As Boolean
        Return c = separator
    End Function

    Protected Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                        ByRef output As value_clause) As Boolean
        Dim name As String = Nothing
        If value_name_defination.parse(input, index, name) AndAlso _
           token(input, index, Nothing, AddressOf isseparator, AddressOf [false]) Then
            Dim v As value = Nothing
            If value.parse(input, index, v) Then
                output = New value_clause(name, v)
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Public Shared Shadows Function parse(ByVal input As String, ByRef index As Int64, _
                                         ByRef output As value_clause) As Boolean
        Return parsebase(input, index, output, AddressOf parseImpl)
    End Function

    Public Overrides Function ToString() As String
        Return left + separator + Convert.ToString(value)
    End Function
End Class
